var resultMonth = 0,
  resultWeek = 0,
  flag = true,
  submitState = false,
  ajaxState = false,
  userName = $('.user-name input'),
  userPhone = $('.user-phone input'),
  cropName = $('.crop-name .select'),
  cropDetailName = $('.crop-name .crop-detail-name input'),
  plantSize = $('.plant-size input'),
  cropYield = $('.crop-yield input'),
  provinceDefault = $('.province-default'),
  cityDefault = $('.city-default'),
  areaDefault = $('.area-default'),
  addressDetail = $('.address-detail-text input'),
  id = 0,
  // ipAddress = 'http://192.168.2.222:82',
  ipAddress = 'https://app.guobener.com',
  provinceId = 0,
  cityId = 0,
  areaId = 0

$('.user-occupation div').click(function () {
  $(this)
    .addClass('user-occupation-active')
    .removeClass('user-occupation-link')
    .siblings()
    .removeClass('user-occupation-active')
    .addClass('user-occupation-link')
})
$('input').focus(function () {
  $(this)
    .parent('div')
    .css({
      border: '1px solid #ADE6C5'
    })
})
$('input').blur(function () {
  $(this)
    .parent('div')
    .css({
      border: '1px solid #D3D3D3'
    })
})

$('.result-date .result-date-month .option-li').click(function () {
  resultMonth = $(this).index()
})
$('.result-date .result-date-week .option-li').click(function () {
  resultWeek = $(this).index()
})
$('.join-main').on('click', function (e) {
  if (
    userName.val().length &&
    userPhone.val().length &&
    cropName.html() != '请选择' &&
    provinceDefault.html() != '请选择' &&
    cityDefault.html() != '请选择' &&
    areaDefault.html() != '请选择' &&
    cropDetailName.val().length &&
    plantSize.val().length &&
    cropYield.val().length &&
    resultMonth != 0 &&
    resultWeek != 0
  ) {
    submitState = true
    $('.submit-btn').css('background', '#30BF6D')
  } else {
    submitState = false
    $('.submit-btn').css('background', '#dadada')
  }
})

$('.submit-btn').click(function (e) {
  e.stopPropagation();
  e.preventDefault();
  if (!submitState) {
    return false
  }
  $('.loading').show()
  submitState = false
  $('.submit-btn').css('background', '#dadada')
  var type = '00'
  var userType = $('.user-occupation-active').html()
  if (userType == '我是经销商') {
    type = '01'
  } else if (userType == '我是农场主') {
    type = '02'
  } else if (userType == '我是合作社') {
    type = '03'
  }
  var data = {}
  data.name = userName.val()
  data.phone = userPhone.val()
  data.provinceId = provinceId
  data.cityId = cityId
  data.countryId = areaId
  data.cropsClass = cropName.html()
  data.cropsName = cropDetailName.val()
  data.acreage = plantSize.val()
  data.yield = cropYield.val()
  data.downFruitMonth = resultMonth
  data.downFruitWeek = resultWeek
  data.type = type
  data.address = addressDetail.val()

  $.ajax({
    type: 'post',
    url: ipAddress + '/wap/join/save',
    data: data,
    dataType: 'json',
    crossDomain: true,
    success: function (data) {
      if (data.result.state == '1') {
        initInfo()
        location.href = 'join-success.html'
      } else {
        alert(data.result.message)
      }
      setTimeout(function () {
        $('.loading').hide()
      }, 1000);
    },
    error: function (err) {
      submitState = true
      $('.loading').hide()
    }
  })

})

function initInfo() {
  userName.val('')
  userPhone.val('')
  cropName.html('请选择')
  cropDetailName.val('')
  plantSize.val('')
  cropYield.val('')
  provinceDefault.html('请选择')
  cityDefault.html('请选择')
  areaDefault.html('请选择')
  submitState = false
  resultMonth = 0
  resultWeek = 0
  $('.result-date .result-date-month .select').html('请选择')
  $('.result-date .result-date-week .select').html('请选择')
}
$('.select').click(function () {
  if (flag) {
    $(this)
      .parent()
      .css({
        'border-radius': '4px 4px 0 0',
        border: '1px solid #ADE6C5'
      })
    $(this)
      .next('.option')
      .css('display', 'block')
    flag = false
    $(this)
      .next('.option')
      .children('.option-li')
      .click(function () {
        $(this)
          .parents('.select-parent')
          .css({
            'border-radius': '4px',
            border: '1px solid #d3d3d3'
          })
        $(this)
          .parent()
          .css('display', 'none')
        $(this)
          .parent()
          .prev()
          .html($(this).html())
        flag = true
      })
  } else {
    $(this)
      .parent()
      .css({
        'border-radius': '4px',
        border: '1px solid #d3d3d3'
      })
    $(this)
      .next('.option')
      .css('display', 'none')
    flag = true
  }
})

$('.user-phone input').blur(function () {
  if (!/^1[34578]\d{9}$/.test($(this).val())) {
    $('.user-phone-tip').css('display', 'block')
    $(this)
      .parent('div')
      .css('border', '1px solid #DC0000')
  } else {
    $('.user-phone-tip').css('display', 'none')
    $(this)
      .parent('div')
      .css('border', '1px solid #D3D3D3')
  }
})


$.ajax({
  type: 'post',
  url: ipAddress + '/wap/join/getRegion?parentId=' + id,
  dataType: 'json',
  success: function (data) {
    let str = ''
    for (let i = 0; i < data.region.length; i++) {
      str +=
        '<li class="province-select-list" id="' +
        data.region[i].id +
        '">' +
        data.region[i].areaname +
        '</li>'
    }
    $('.province-select').html(str)
    $('.province-select-list').click(function () {
      provinceId = $(this).attr('id')
      cityId = 0
      areaId = 0
      $(this)
        .parent()
        .css('display', 'none')
      $(this)
        .parent()
        .parent()
        .css({
          'border-radius': '4px',
          border: '1px solid #d3d3d3'
        })
      flag = true
      $('.province-default').html($(this).html())
      $('.area-default').html('请选择')
      $('.city-default').html('请选择')
      $.ajax({
        type: 'post',
        url: ipAddress + '/wap/join/getRegion?parentId=' + provinceId,
        dataType: 'json',
        success: function (data) {
          let str = ''
          for (let i = 0; i < data.region.length; i++) {
            str +=
              '<li class="city-select-list" id="' +
              data.region[i].id +
              '">' +
              data.region[i].areaname +
              '</li>'
          }
          $('.city-select').html(str)
          $('.city-select-list').click(function () {
            cityId = $(this).attr('id')
            areaId = 0
            $(this)
              .parent()
              .css('display', 'none')
            $(this)
              .parent()
              .parent()
              .css({
                'border-radius': '4px',
                border: '1px solid #d3d3d3'
              })
            flag = true
            $('.city-default').html($(this).html())
            $('.area-default').html('请选择')
            $.ajax({
              type: 'post',
              url: ipAddress + '/wap/join/getRegion?parentId=' + cityId,
              dataType: 'json',
              success: function (data) {
                let str = ''
                for (let i = 0; i < data.region.length; i++) {
                  str +=
                    '<li class="area-select-list" id="' +
                    data.region[i].id +
                    '">' +
                    data.region[i].areaname +
                    '</li>'
                }
                $('.area-select').html(str)
                $('.area-select-list').click(function () {
                  areaId = $(this).attr('id')
                  $(this)
                    .parent()
                    .css('display', 'none')
                  $(this)
                    .parent()
                    .parent()
                    .css({
                      'border-radius': '4px',
                      border: '1px solid #d3d3d3'
                    })
                  flag = true
                  $('.area-default').html($(this).html())
                })
              }
            })
          })
        }
      })
    })
  }
})
